package com.test.qpbgboot.base.rabbitmq;

import com.rabbitmq.client.Channel;
import com.test.qpbgboot.base.constants.RabbitMQConstants;
import com.test.qpbgboot.schedule.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RabbitMQConsumer {

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues =
            RabbitMQConstants.ORDER_WAITING_PAY_DEAD_QUEUE)
    public void receive(String message, Message enMessage, Channel channel) {
        log.debug("监听-RabbitMQ延迟消息-付款检查 message:{}", message);

        log.debug("监听-RabbitMQ延迟消息-付款检查 " +
                "检查-是否已经付款 入参:{}", message);
        boolean success = orderService.checkOrderPaid(Long.valueOf(message));
        log.debug("监听-RabbitMQ延迟消息-付款检查 " +
                "检查-是否已经付款 入参:{},出参:{}", message, success);

        //通知RabbitMQ消息处理完成，删除该条消息
        try {
            channel.basicAck(enMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.warn("通知-消息处理完成-失败");
        }
    }
}
